﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class FansBll:IFansBll
    {
        private IFansDal _dal;

        public FansBll(IFansDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddFansAsync(int Mid, int Cid, DateTime FocusTime)
        {
            return await _dal.AddModelAsync(new Fans
            {
                mid = Mid,
                cid = Cid,
                FocusTime = FocusTime
            });
        }

        public async Task<int> DeleteFansAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditFansAsync(int Id, int Mid, int Cid, DateTime FocusTime)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.mid = Mid;
            data.cid = Cid;
            data.FocusTime = FocusTime;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<FansDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new FansDtos
            {
                id = r.Id,
                mid = r.mid,
                cid = r.cid,
                FocusTime = r.FocusTime,
               
            }) : new List<FansDtos>();
        }

        public async Task<FansDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new FansDtos() : new FansDtos
            {
                id = data.Id,
                mid = data.mid,
                cid = data.cid,
                FocusTime = data.FocusTime,
                
            };
        }
    }
}
